package com.example.springbooteasycode.utils.qrcode.withText;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class QRCodeWithTextGenerator0 {

    public static void main(String[] args) {
        String qrData = "https://example.com";
        int qrWidth = 400;
        int qrHeight = 400;
        String outputFileName = "qrcode_with_text.png";

        try {
            // 生成二维码图片
            BufferedImage qrCodeImage = generateQRCode(qrData, qrWidth, qrHeight);

            // 在二维码图片上添加文字信息
            BufferedImage finalImage = addTextToQRCode(qrCodeImage, qrWidth, qrHeight);

            // 保存最终的图片
            File outputFile = new File(outputFileName);
            ImageIO.write(finalImage, "png", outputFile);

            System.out.println("QR code with text generated and saved as: " + outputFile.getAbsolutePath());
        } catch (IOException | WriterException e) {
            e.printStackTrace();
        }
    }

    public static BufferedImage generateQRCode(String data, int width, int height) throws WriterException, IOException {
        QRCodeWriter qrCodeWriter = new QRCodeWriter();
        Map<EncodeHintType, Object> hints = new HashMap<>();
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
        BitMatrix bitMatrix = qrCodeWriter.encode(data, BarcodeFormat.QR_CODE, width, height, hints);

        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics2D graphics = image.createGraphics();
        graphics.setColor(Color.WHITE);
        graphics.fillRect(0, 0, width, height);
        graphics.setColor(Color.BLACK);
        for (int x = 0; x < width; x++) {
            for (int y = 0; y < height; y++) {
                if (bitMatrix.get(x, y)) {
                    graphics.fillRect(x, y, 1, 1);
                }
            }
        }
        graphics.dispose();

        return image;
    }

//    public static BufferedImage generateQRCode(String data, int width, int height, int margin) throws WriterException, IOException {
//        QRCodeWriter qrCodeWriter = new QRCodeWriter();
//        Map<EncodeHintType, Object> hints = new HashMap<>();
//        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
//        BitMatrix bitMatrix = qrCodeWriter.encode(data, BarcodeFormat.QR_CODE, width, height, hints);
//
//        // 增加边距后的实际宽度和高度
//        int totalWidth = width + 2 * margin;
//        int totalHeight = height + 2 * margin;
//
//        BufferedImage image = new BufferedImage(totalWidth, totalHeight, BufferedImage.TYPE_INT_RGB);
//        Graphics2D graphics = image.createGraphics();
//        graphics.setColor(Color.WHITE);
//        graphics.fillRect(0, 0, totalWidth, totalHeight);
//        graphics.setColor(Color.BLACK);
//
//        // 计算二维码绘制的起始位置
//        int startX = margin;
//        int startY = margin;
//
//        for (int x = 0; x < width; x++) {
//            for (int y = 0; y < height; y++) {
//                if (bitMatrix.get(x, y)) {
//                    graphics.fillRect(startX + x, startY + y, 1, 1);
//                }
//            }
//        }
//
//        graphics.dispose();
//
//        return image;
//    }

    public static BufferedImage addTextToQRCode(BufferedImage qrCodeImage, int width, int height) {
        BufferedImage finalImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics2D g2d = finalImage.createGraphics();
        g2d.drawImage(qrCodeImage, 0, 0, null);
        g2d.setFont(new Font("SimSun", Font.PLAIN, 24)); // 使用宋体字体
        g2d.setColor(Color.BLACK);

        // 设置文字的位置
        g2d.drawString("编号: 8924062617520001259104", 10, height - 30);
        g2d.drawString("名称: A5", 10, height - 10);
        g2d.drawString("位置: 1号楼-1单元-2层-201室", 10, height + 10);

        g2d.dispose();
        return finalImage;
    }
}
